package com.ruoyi.web.controller.admin;


import com.ruoyi.admin.domain.LlmDatasets;
import com.ruoyi.admin.service.ILlmDatasetsService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * 知识库控制器
 */
@RestController
@RequestMapping("/datasets")
@Slf4j
@RequiredArgsConstructor
public class LlmDatasetsCollection extends BaseController {


    private final ILlmDatasetsService llmDatasetServiceImpl;


    /**
     * 新增知识库
     *
     * @param dataset 新增实体
     * @return ajaxR
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LlmDatasets dataset) {
        return llmDatasetServiceImpl.creatDataset(dataset) ? success("创建成功") : error("创建失败");
    }


    /**
     * 删除知识库
     *
     * @param id 知识库id
     * @return ajaxR
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable(name = "id") String id) throws InterruptedException {
        return llmDatasetServiceImpl.deleteDataset(id) ? success("删除成功") : error("删除失败");
    }

    /**
     * 修改知识库
     *
     * @param dataset 知识库实体
     * @return ajaxR
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LlmDatasets dataset) {
        return llmDatasetServiceImpl.updateDataset(dataset) ? success("修改成功") : error("修改失败");
    }

    /**
     * 分页查询知识库
     *
     * @param datasetName 知识库名称
     * @param dataSetCode 知识库code
     * @return tableData
     */
    @GetMapping("/index")
    public TableDataInfo index(@RequestParam(name = "datasetName", defaultValue = "") String datasetName,
                               @RequestParam(name = "dataSetCode", defaultValue = "") String dataSetCode,
                               @RequestParam(name = "keywords", defaultValue = "") String keywords) {
        startPage();
        return getDataTable(llmDatasetServiceImpl.getDatasets(datasetName, dataSetCode,keywords));
    }

    /**
     * 根据id 获取文件夹的树形结构
     *
     * @param id 顶级di
     * @return tree
     */
    @GetMapping("/getDatasetTreeById/{id}")
    public AjaxResult getDatasetTreeById(@PathVariable("id") String id) {
        return AjaxResult.success(llmDatasetServiceImpl.getDatasetTreeById(id));
    }

    /**
     * 通过id 获取详细信息
     *
     * @param id 知识库id
     * @return ajax
     */
    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable("id") String id) {

        return AjaxResult.success(llmDatasetServiceImpl.getDatasetById(id));
    }
}
